package com.iplatform.iplatformuser.privilege.service.impl;

import com.iplatform.iplatformuser.privilege.dao.PriManagerMapper;
import com.iplatform.iplatformuser.privilege.service.inter.IPriManagerService;
import com.iplatform.module.privilege.PriManagerBean;
import com.iplatform.module.privilege.PriMenuBean;
import com.iplatform.module.privilege.dto.PriManagerDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>Title: PriManagerServiceImpl</p>
 * <p>Description: </p>
 *
 * @author RayLLi
 * @date 2018年8月27日
 */
@Service
public class PriManagerServiceImpl implements IPriManagerService {

    @Autowired
    private PriManagerMapper userManagerMapper;

    @Override
    public PriManagerBean checkManager(PriManagerBean priManager) {

        priManager = userManagerMapper.getManagerByCodeAndPassword(priManager);
        Map<String, String> map = null;
        if (priManager != null) {
            //判断是否为超级管理员
            if ("1".equals(priManager.getIsSuper())) {
                map = userManagerMapper.queryHomeUrlForAdmin();
            } else {
                map = userManagerMapper.queryHomeUrl(priManager.getUserId());
            }
            //判断是否有首页
            if (map != null) {
                priManager.setHomeUrl(map.get("menu_link"));
                priManager.setFloatHomeUrl(map.get("float_link"));
            } else {
                priManager.setHomeUrl("home-404");
                priManager.setFloatHomeUrl("home-404");
            }
        }

        return priManager;
    }

    @Override
    public Map<String, String> getUserRole(String mgrId) {
        return userManagerMapper.getUserRole(mgrId);
    }

    @Override
    public List<PriMenuBean> queryAllMenu() {
        return userManagerMapper.loadAllMenu();
    }

    @Override
    public List<Map<String, String>> queryMenuAndOptByUser(PriManagerBean priManager) {
        return userManagerMapper.queryMenuAndOptByUser(priManager);
    }


    @Transactional
    public void deleteManager(String mgrId) {
        userManagerMapper.deleteManager(mgrId);
    }


    @Override
    public String queryHomeUrl(String mgrId) {
        Map<String, String> map = userManagerMapper.queryHomeUrl(mgrId);
        return map.get("menu_link");
    }


    @Override
    public List<PriManagerDto> loadManagers(Map<String, String> formMap) {
        List<PriManagerDto> list = userManagerMapper.loadManagers(formMap);
        for (PriManagerDto priManagerDto : list){
            priManagerDto.setRoles(userManagerMapper.findRoles(priManagerDto.getMgrId()));
        }
        return list;
    }

    @Override
    public List<PriManagerBean> queryByCode(Map<String, String> formMap) {
        return userManagerMapper.queryByCode(formMap);
    }

    @Override
    @Transactional
    public int insert(PriManagerDto priManagerDto) {
        useOrderId(priManagerDto);
        return userManagerMapper.insertManager(priManagerDto);
    }

    public void useOrderId(PriManagerDto priManagerDto){
        List<BigDecimal> ords = userManagerMapper.findOrds(priManagerDto.getMgrId());
        if (ords.contains(priManagerDto.getOrderId())){
            List<String> mgrIds = userManagerMapper.findMgrIds(priManagerDto.getOrderId(),priManagerDto.getMgrId());
            userManagerMapper.managerOrderAddOne(mgrIds);
        }
    }

    @Transactional
    public void batchDeleteManager(String[] mgrIds) {
        userManagerMapper.batchDeleteManager(mgrIds);
    }

    @Transactional
    public void setStopOrActive(String mgrId) {
        userManagerMapper.setStopOrActive(mgrId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int update(PriManagerDto priManagerDto) {
        useOrderId(priManagerDto);
        if (priManagerDto.getRoleIds().size() > 0){
            String[] mgrIds = new String[]{priManagerDto.getMgrId()};
            userManagerMapper.delManagerRoleId(mgrIds);
            for (String roleId : priManagerDto.getRoleIds()){
                userManagerMapper.binds(priManagerDto.getMgrId(),roleId);
            }
        }
        return userManagerMapper.updateManager(priManagerDto);
    }

    @Override
    public int checkAdd(String userCode) {
        return userManagerMapper.checkAdd(userCode);
    }

    @Override
    public int checkUpdate(String userCode, String mgrId) {
        return userManagerMapper.checkUpdate(userCode, mgrId);
    }

    @Override
    public PriManagerDto queryByMgrId(String mgrId) {
        return userManagerMapper.queryByMgrId(mgrId);
    }

    @Override
    public void binds(String mgrId,List<String> roleIds) {
        for (String roleId : roleIds){
            userManagerMapper.binds(mgrId,roleId);
        }
    }

    @Override
    public List<Map<String, String>> queryCodeAndName() {
        return userManagerMapper.queryCodeAndName();
    }

}
